package com.itheima.sfbx.insurance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.itheima.sfbx.framework.commons.utils.EmptyUtil;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.framework.commons.utils.ExceptionsUtil;
import com.itheima.sfbx.framework.commons.exception.ProjectException;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description：系数项服务实现类
 */
@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {

    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<Coefficent> queryWrapper = queryWrapper(coefficentVO);

            // 构建分页对象
            Page<Coefficent> coefficentPage = page(new Page<>(pageNum, pageSize), queryWrapper);
            return BeanConv.toPage(coefficentPage, CoefficentVO.class);
        } catch (Exception e) {
            log.error("系数项分页查询异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.PAGE_FAIL);
        }
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        return null;
    }

    @Override
    public CoefficentVO save(CoefficentVO coefficentVO) {
        try {
            // coefficentVO转换为coefficent
            Coefficent coefficent = BeanConv.toBean(coefficentVO, Coefficent.class);

            //保存coefficent
            boolean flag = save(coefficent);

            if (!flag){
                throw new RuntimeException("保存系数项失败");
            }

            // 再转换为coefficentVO返回
            return BeanConv.toBean(coefficent,CoefficentVO.class);
        } catch (RuntimeException e) {
            log.error("系数项保存异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
    }

    @Override
    public Boolean update(CoefficentVO coefficentVO) {
        try {
            // coefficentVO转换为coefficent
            Coefficent coefficent = BeanConv.toBean(coefficentVO, Coefficent.class);

            // 修改coefficent
            boolean flag = updateById(coefficent);
            if (!flag){
                throw new RuntimeException("修改系数项失败！");
            }
            return flag;
        } catch (RuntimeException e) {
            log.error("系数项修改异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
    }

    @Override
    public Boolean delete(String[] checkedIds) {
        try {
            List<Long> idsLong = Arrays.stream(checkedIds).map(Long::parseLong).collect(Collectors.toList());
            boolean flag = removeByIds(idsLong);
            if (!flag){
                throw new RuntimeException("批量删除系数项失败");
            }
            return flag;
        } catch (RuntimeException e) {
            log.error("系数项删除异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.DEL_FAIL);
        }
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<Coefficent> queryWrapper = queryWrapper(coefficentVO);
            // 查询系数项列表
            List<Coefficent> coefficentList = list(queryWrapper);
            // 转为coefficenVOList
            if (CollectionUtils.isNotEmpty(coefficentList)){
                List<CoefficentVO> coefficentVOList = BeanConv.toBeanList(coefficentList, CoefficentVO.class);
                return coefficentVOList;
            }
        } catch (Exception e) {
            log.error("系数项列表查询异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(CoefficentEnum.LIST_FAIL);
        }
        return List.of();
    }

    private LambdaQueryWrapper<Coefficent> queryWrapper(CoefficentVO coefficentVO) {
        LambdaQueryWrapper<Coefficent> queryWrapper = new LambdaQueryWrapper<>();
        // 构建查询条件
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKey()),
                Coefficent::getCoefficentKey, coefficentVO.getCoefficentKey());
        queryWrapper.like(!EmptyUtil.isNullOrEmpty(coefficentVO.getCoefficentKeyName()),
                Coefficent::getCoefficentKeyName, coefficentVO.getCoefficentKeyName());
        queryWrapper.eq(!EmptyUtil.isNullOrEmpty(coefficentVO.getDataState()),
                Coefficent::getDataState, coefficentVO.getDataState());
        return queryWrapper;
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        return null;
    }
}
